# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 读取csv文件.py
Author: kij
Create Date: 2021/2/9
-------------------------------------------------
"""
import os
import requests


# 获取文件
def get_file_path(file_name):
	return os.path.join(os.path.dirname(os.path.abspath(__file__)), "files", file_name)


def download_file(file_url, user_name):
	print(user_name,file_url)
	res = requests.get(
		url=file_url,
		headers={
			"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36"
		}
	)
	if not os.path.exists(get_file_path("images")):
		# 创建文件路径
		os.makedirs(get_file_path("images"))
	with open(get_file_path("images/{}.png".format(user_name)), mode="wb") as img_object:
		# 拿到字节数据然后写入
		img_object.write(res.content)


with open(get_file_path("mv.csv"), mode='r', encoding='utf-8') as file_object:
	file_object.readline()
	for line in file_object:
		list_url = line.strip().split(",")
		print(list_url[2])
		download_file(user_name= str(list_url[1]),file_url=str(list_url[2]))
